<template>
    <div>
      <el-form v-if="!searchState" size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
         <!-- <el-form-item prop="name">
                <el-input size="small" v-model="searchForm.name" placeholder="姓名" clearable></el-input>
         </el-form-item>
         <el-form-item prop="currentPosition">
                <el-input size="small" v-model="searchForm.currentPosition" placeholder="现任职务" clearable></el-input>
         </el-form-item>
         <el-form-item prop="evaluate">
                <el-input size="small" v-model="searchForm.evaluate" placeholder="总体评价" clearable></el-input>
         </el-form-item>
         <el-form-item prop="remarks">
                <el-input size="small" v-model="searchForm.remarks" placeholder="备注信息" clearable></el-input>
         </el-form-item> -->
         <el-form-item label="年" prop="createdate">
           <el-input-number v-model="createdate" controls-position="right" :min="2021" :max="9999"></el-input-number>
         </el-form-item>
         <el-form-item label="投票次数" prop="ticketCount">
           <el-input-number v-model="ticketCount" controls-position="right" :min="1" :max="9999"></el-input-number>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <!-- <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form size="small" :inline="true" v-show="isImportCollapse"  ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/ticket/samelevelteach/samelevelTeach/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog> -->
      <div class="bg-white top">
      <div align="center" style="font-size: 26px;">{{new Date().getFullYear() + '年度中层干部考核民主测评表'}}</div>
      <br>
      <el-row>
        <!-- <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
        <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button> -->
        <el-button-group class="pull-right">
            <!-- <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse"></el-button>
            <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button> -->
            <!-- <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button> -->
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
       size="small"
       style="border: 2px solid #76838F"
      :header-cell-style="headerCellStyle"
      :cell-style="cellStyle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <!-- @selection-change="selectionChangeHandle" -->
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
    <el-table-column
      label="序号"
      width="100px"
      align="center"
      type="index">
      </el-table-column>
    <el-table-column
        prop="name"
        show-overflow-tooltip
        align="center"
        label="姓名">
      </el-table-column>
    <el-table-column
        prop="currentPosition"
        align="left"
        header-align="center"
        min-width="200px"
        class="cell"
        label="现任职务">
      </el-table-column>
    <el-table-column
        show-overflow-tooltip
        align="center"
        label="总体评价">
        <el-table-column
            prop="remarks"
            align="center"
            show-overflow-tooltip
            label="优秀">
            <template slot-scope="scope">
              <el-radio-group v-model="scope.row.remarks">
                <el-radio-button label="优秀" border>优秀</el-radio-button>
              </el-radio-group>
            </template>
          </el-table-column>
        <el-table-column
            prop="remarks"
            align="center"
            show-overflow-tooltip
            label="称职">
            <template slot-scope="scope">
              <el-radio-group v-model="scope.row.remarks">
                <el-radio-button label="称职" border>称职</el-radio-button>
              </el-radio-group>
            </template>
          </el-table-column>
        <el-table-column
            prop="remarks"
            align="center"
            show-overflow-tooltip
            label="基本称职">
            <template slot-scope="scope">
              <el-radio-group v-model="scope.row.remarks">
                <el-radio-button label="基本称职" border>基本称职</el-radio-button>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column
              prop="remarks"
              align="center"
              show-overflow-tooltip
              label="不称职">
              <template slot-scope="scope">
                <el-radio-group v-model="scope.row.remarks">
                  <el-radio-button label="不称职" border>不称职</el-radio-button>
                </el-radio-group>
              </template>
            </el-table-column>
      </el-table-column>
    <!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息">
      </el-table-column> -->
      <!-- <el-table-column
        v-if="hasPermission('ticket:samelevelteach:samelevelTeach:del')"
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('ticket:samelevelteach:samelevelTeach:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination> -->
    <br>
    <div align="center" style="font-size: 26px;">{{pageNo + '/' + total + '页'}}</div>
    <br>
    <el-row style="margin-left: 45%;">
      <el-button v-show="pageNo === 1 && subState" type="primary" size="big" @click="jumpUp()">上一页</el-button>
      <el-button class="button1" v-show="pageNo !== 1" type="primary" size="big" @click="pageCount('pre')">上一页</el-button>
      <el-button class="button2" v-show="pageNo !== total" type="primary" size="big" @click="pageCount('next')">下一页</el-button>
      <el-button v-show="pageNo === total && submitState && searchState" type="primary" size="big" @click="submit()">提交</el-button>
    </el-row>
    </div>
    <asyn-for-tool ref='asynForTool' @returnData='returnData(arguments)'></asyn-for-tool>
        <!-- 弹窗, 新增 / 修改 -->
    <!-- <SamelevelTeachForm  ref="samelevelTeachForm" @refreshDataList="refreshList"></SamelevelTeachForm> -->
  </div>
</template>

<script>
  // import SamelevelTeachForm from './SamelevelTeachForm'
  import {clearLoginInfo} from '@/utils'
  export default {
    data () {
      return {
        searchForm: {
          name: '',
          currentPosition: '',
          remarks: '',
          ticketCount: ''
        },
        createdate: new Date().getFullYear(),
        ticketCount: 1,
        state: false,
        loadings: '',
        subState: false,
        dataList: [],
        dataLists: [],
        dataArr: [],
        errList: '', // 未填写单位
        pageNo: 1,
        pageSize: 5000,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isImportCollapse: false,
        loading: false,
        redisCount: 0 // redis存储计数
      }
    },
    components: {
      // SamelevelTeachForm
    },
    watch: {
      'dataLists': {
        handler (newVal, oldVal) {
          this.redisCount++
          if (this.redisCount >= 3) {
            this.$http({
              url: '/peopleinformation/administrativelevel/peopleAdministrativeLevel/Redisset',
              method: 'post',
              data: {
                key: (localStorage.getItem('loginName') + this.$route.path),
                value: JSON.stringify(newVal)
              }
            })
            this.redisCount = 0
          }
        },
        deep: true
      }
    },
    computed: {
      submitState () {
        return this.subState && localStorage.getItem(localStorage.getItem('loginName') + 'submit') === null
      },
      searchState () {
        return localStorage.getItem('loginName') !== 'admin'
      }
    },
    activated () {
      this.refreshList()
    },
    methods: {
      headerCellStyle ({row}) {
        return "font-size: 24px;border: 1px solid black;font-weight:bold;"
      },
      cellStyle ({row, column, rowIndex, columnIndex}) {
        if (this.state) {
          if (row.remarks === "" || row.remarks === undefined) {
            return "background:#d3d3d3;border:2px;color: #FFFFFF;font-size: 20px;border: 1px solid black;"
          }
        }
        return "font-size: 20px;border: 1px solid black;"
      },
      // 提交成功
      submitSuccess () {
        this.$http({
          url: '/peopleinformation/administrativelevel/peopleAdministrativeLevel/Redisset',
          method: 'post',
          data: {
            key: (localStorage.getItem('loginName') + 'state'),
            value: '已填写'
          }
        })
      },
      jumpUp () {
        this.$router.push('/ticket/samelevelleadership/SamelevelLeadershipList')
      },
      // 退出登录
      out () {
        this.$http({
          url: '/sys/logout',
          method: 'get'
        }).then(({data}) => {
          if (data && data.success) {
            clearLoginInfo()
            if (process.env.VUE_APP_SSO_LOGIN === 'true') {
              let service = window.location.protocol + '//' + window.location.host + '/'
              window.location.href = `${process.env.VUE_APP_CAS_SERVER}/logout?service=${service}`
              this.$tool.flash()
            } else {
              this.$router.replace({name: 'login'})
              this.$tool.flash()
            }
          }
        })
      },
      // 获取数据列表
      refreshList () {
        this.subState = false
        this.loading = true
        this.redisCount = 0
        this.dataLists = []
        this.dataList = []
        if (localStorage.getItem(localStorage.getItem('loginName') + 'submit') !== null) {
          this.out()
        } else {
        if (localStorage.getItem('loginName') === 'admin') {
          if (this.createdate === '' || this.createdate === null || this.createdate === undefined || this.ticketCount === undefined || this.ticketCount === undefined || this.ticketCount === undefined) {
            this.searchForm.ticketCount = new Date().getFullYear() + '-' + 1
          } else {
            this.searchForm.ticketCount = this.createdate + '-' + this.ticketCount
          }
          this.searchForm.admin = 'admin'
        } else {
          this.searchForm.ticketCount = localStorage.getItem('checkTime')
          this.searchForm.admin = ''
        }
        this.searchForm.key = localStorage.getItem('loginName') + this.$route.path
        this.$http({
          url: 'ticket/samelevelteach/samelevelTeach/list',
          method: 'get',
          params: {
            'pageSize': 9999,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.pageNo = 1
            // this.total = data.page.count
            if (data.msg === 'redis') {
              this.dataLists = JSON.parse(data.value)
              this.dataList = this.dataLists.slice(0, 10)
              if (this.dataLists.length % 10 === 0) {
                this.total = Math.floor(this.dataLists.length / 10)
              } else {
                this.total = Math.floor(this.dataLists.length / 10) + 1
              }
            } else {
              this.dataLists = data.page.list
              this.dataList = this.dataLists.slice(0, 10)
              if (this.dataLists.length % 10 === 0) {
                this.total = Math.floor(this.dataLists.length / 10)
              } else {
                this.total = Math.floor(this.dataLists.length / 10) + 1
              }
            }
            this.subState = true
            this.loading = false
          }
        })
        }
      },
      // 当前页校验
      pageValidation () {
        this.errList = ''
        for (var i in this.dataList) {
          if (this.dataList[i].remarks === '' || this.dataList[i].remarks === undefined) {
            this.errList += " " + this.dataList[i].name
            break
          }
        }
      },
      // 页码
      pageCount (val) {
        var valPage = ''
        if (val === 'next') {
          this.pageValidation()
        }
        if (localStorage.getItem('loginName') !== 'admin') {
          this.dataArr = []
          var height = 0
          var heightList = document.getElementsByClassName('el-table__row')
          if (this.errList !== '') {
            this.state = true
            for (let k = 0; k < this.dataList.length; k++) {
              if (this.dataList[k].remarks === '' || this.dataList[k].remarks === undefined) { // 不是空串
                break
              } else {
                height += heightList[k].offsetHeight
              }
            }
            window.scrollTo(0, 10 + 80 + 90.56 + height)
          } else {
            this.state = false
            if (val === 'next') {
              if (this.pageNo === this.total) {
                alert(`当前是最后一页`)
              } else {
                valPage = this.pageNo + 1
                this.currentChangeHandle(valPage)
              }
            } else {
              if (this.pageNo === 1) {
                alert(`当前是第一页`)
              } else {
                valPage = this.pageNo - 1
                this.currentChangeHandle(valPage)
              }
            }
          }
      } else {
        if (val === 'next') {
          if (this.pageNo === this.total) {
            alert(`当前是最后一页`)
          } else {
            valPage = this.pageNo + 1
            this.currentChangeHandle(valPage)
          }
        } else {
          if (this.pageNo === 1) {
            alert(`当前是第一页`)
          } else {
            valPage = this.pageNo - 1
            this.currentChangeHandle(valPage)
          }
        }
      }
      },
      // 提交数据
      submit () {
           this.errList = ''
          this.dataArr = []
          var height = 0
          var heightList = document.getElementsByClassName('el-table__row')
          for (let i in this.dataList) {
            if (this.dataList[i].remarks === '' || this.dataList[i].remarks === undefined) {
              this.errList += " " + this.dataList[i].unit
            }
          }
          if (this.errList !== '') {
            this.state = true
            for (let k = 0; k < this.dataList.length; k++) {
              if (this.dataList[k].remarks === '' || this.dataList[k].remarks === undefined) { // 不是空串
                break
              } else {
                height += heightList[k].offsetHeight
              }
            }
            window.scrollTo(0, 10 + 80 + 90.56 + height)
         } else {
           for (var l in this.dataLists) {
               this.$set(this.dataLists[l], 'tableName', 'samelevel_teach')
               this.dataArr.push(this.lodash.pick(this.dataLists[l], 'remarks', 'name', 'mark', 'tableName', 'identity', 'currentPosition', 'ticketCount'))
           }
             this.loading = true
             localStorage.setItem(localStorage.getItem('loginName') + 'submit', '1')
             this.$http({
               url: `/ticketdata/leadershipdata/leadershipData/manysave`,
               method: 'post',
               data: {
                 account: localStorage.getItem('loginName'),
                 finishState: '已完成',
                 middlerleader: this.dataArr,
                 unitleader: this.$route.query.dataArr
               }
             })
             this.submitSuccess()
             this.out()
           }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        if (this.errList === '' || localStorage.getItem('loginName') === 'admin') {
          this.$http({
            url: '/peopleinformation/administrativelevel/peopleAdministrativeLevel/Redisset',
            method: 'post',
            data: {
              key: (localStorage.getItem('loginName') + this.$route.path),
              value: JSON.stringify(this.dataLists)
            }
          })
          this.pageNo = val
          this.dataList = []
          this.redisCount = 0
          this.dataList = this.dataLists.slice((val - 1) * 10, (val - 1) * 10 + 10)
          window.scrollTo(0, 0)
        }
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.samelevelTeachForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.samelevelTeachForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.samelevelTeachForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/ticket/samelevelteach/samelevelTeach/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/ticket/samelevelteach/samelevelTeach/import/template')
      },
      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/ticket/samelevelteach/samelevelTeach/export', params)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style type="text/css">
  .msgBox{
    width: auto !important;
  }
  .table .cell {
      white-space: pre-wrap;
  }
  .loadingStyle {
    font-size: 32px;
  }
</style>
